import Vue from "vue";

import axios from "axios";
import VueAxios from "vue-axios";
axios.defaults.baseURL = "http://127.0.0.1:8001/";
axios.interceptors.request.use((config) => {
  // console.log(config);
  config.headers.Authorization = sessionStorage.getItem("token");
  return config;
});
Vue.prototype.axios = axios;
Vue.use(VueAxios, axios);

const getDefaultState = () => {
  return {
    name: "",
    role_id: 0,
  };
};

export default {
  namespaced: true,
  state: getDefaultState(),
  mutations: {
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_POWER: (state, role_id) => {
      state.role_id = role_id;
    },
    RESET_STATE: (state) => {
      Object.assign(state, getDefaultState());
      // console.log(state.name);
    },
  },
  actions: {
    ReSet({ commit }) {
      commit("RESET_STATE");
    },
    Loadinginfo({ commit }, name) {
      // return new Promise((resolve) => {
      return (async function () {
        const rres = await Vue.axios({
          method: "get",
          url: `users/${name}`,
        });
        // console.log("roleid", rres.data.role_id);
        commit("SET_NAME", rres.data.name);
        commit("SET_POWER", rres.data.role_id);

        return rres;
        // resolve(rres);
      })();
      // a();
      // });
    },
  },
};
